<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateOrderGoodsAPIRequest;
use App\Http\Requests\API\UpdateOrderGoodsAPIRequest;
use App\Models\OrderGoods;
use App\Repositories\OrderGoodsRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class OrderGoodsController
 * @package App\Http\Controllers\API
 */

class OrderGoodsAPIController extends AppBaseController
{
    /** @var  OrderGoodsRepository */
    private $orderGoodsRepository;

    public function __construct(OrderGoodsRepository $orderGoodsRepo)
    {
        $this->orderGoodsRepository = $orderGoodsRepo;
    }

    /**
     * Display a listing of the OrderGoods.
     * GET|HEAD /orderGoods
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->orderGoodsRepository->pushCriteria(new RequestCriteria($request));
        $this->orderGoodsRepository->pushCriteria(new LimitOffsetCriteria($request));
        $orderGoods = $this->orderGoodsRepository->all();

        return $this->sendResponse($orderGoods->toArray(), 'Order Goods retrieved successfully');
    }

    /**
     * Store a newly created OrderGoods in storage.
     * POST /orderGoods
     *
     * @param CreateOrderGoodsAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateOrderGoodsAPIRequest $request)
    {
        $input = $request->all();

        $orderGoods = $this->orderGoodsRepository->create($input);

        return $this->sendResponse($orderGoods->toArray(), 'Order Goods saved successfully');
    }

    /**
     * Display the specified OrderGoods.
     * GET|HEAD /orderGoods/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var OrderGoods $orderGoods */
        $orderGoods = $this->orderGoodsRepository->findWithoutFail($id);

        if (empty($orderGoods)) {
            return $this->sendError('Order Goods not found');
        }

        return $this->sendResponse($orderGoods->toArray(), 'Order Goods retrieved successfully');
    }

    /**
     * Update the specified OrderGoods in storage.
     * PUT/PATCH /orderGoods/{id}
     *
     * @param  int $id
     * @param UpdateOrderGoodsAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateOrderGoodsAPIRequest $request)
    {
        $input = $request->all();

        /** @var OrderGoods $orderGoods */
        $orderGoods = $this->orderGoodsRepository->findWithoutFail($id);

        if (empty($orderGoods)) {
            return $this->sendError('Order Goods not found');
        }

        $orderGoods = $this->orderGoodsRepository->update($input, $id);

        return $this->sendResponse($orderGoods->toArray(), 'OrderGoods updated successfully');
    }

    /**
     * Remove the specified OrderGoods from storage.
     * DELETE /orderGoods/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var OrderGoods $orderGoods */
        $orderGoods = $this->orderGoodsRepository->findWithoutFail($id);

        if (empty($orderGoods)) {
            return $this->sendError('Order Goods not found');
        }

        $orderGoods->delete();

        return $this->sendResponse($id, 'Order Goods deleted successfully');
    }
}
